const config = require('./config'),
      path = require('path'),
      webpack = require('webpack'),
      extractTextPlugin = require('extract-text-webpack-plugin'),
      miniCssExtractPlugin = require('mini-css-extract-plugin'),
      optimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'),
      htmlPlugin = require('html-webpack-plugin');

// webpack 开发模式 [true|false]
const devMode = true;

module.exports = {
  // 模式
  mode: devMode ? 'development' : 'production',

  // 入口
  entry:  {
    'main': __dirname + '/src/index.js'
  },

  // 输出
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'static/js/[name].js'
  },

  module: {
    rules: [
      // 解析 .scss 文件
      {
        test: /\.scss$/,
        use: [
          miniCssExtractPlugin.loader,
          'css-loader?minimize=true',
          'sass-loader'
        ]
        /*use: extractTextPlugin.extract({
          use: [{
              loader: 'css-loader', options: { minimize: true }
            }, {
              loader: 'sass-loader', options: { minimize: true }
            }
          ],
          // 开发模式下使用 style-loader
          fallback: 'style-loader'
        })*/
      },

      // 图片 loader
      {
        test: /\.(png|jpg|gif|jpeg)/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 8192,
            name: '[hash].[ext]',
            outputPath: 'static/images',
          }
        }]
      },
    ]
  },

  // webpack-dev-server 配置 [ https://webpack.js.org/configuration/dev-server/ ]
  devServer: {
    contentBase: path.resolve(__dirname, 'dist'),
    compress: true, //压缩
    port: 800, // 端口
    open: true // 自动打开浏览器
  },

  // 插件
  plugins: [

    new htmlPlugin({
      filename: 'index.html',
      template: config.src + 'index.html',
      inject: true,
      hash: true,
      date: new Date()
    }),

    // css 提取插件
    new miniCssExtractPlugin({
      filename: 'static/css/[name].css?[hash]',
      chunkFilename: '[name].css?[hash]',
    }),

    // 压缩 css
    new optimizeCssAssetsPlugin(),




    /*new extractTextPlugin({
      filename: 'css/[name].css?[hash]-[chunkhash]',
      // disable: false,
      // allChunks: true
    })*/



  ],

  // 生成Source Maps
  devtool: 'none'
};


/**
 * 时间格式化
 *
 * @param {string} fmt 时间显示
 * @return string
 */
// Date.prototype.Format = function(fmt)
// {
//   let o = {
//     "M+" : this.getMonth()+1,
//     "d+" : this.getDate(),
//     "h+" : this.getHours(),
//     "m+" : this.getMinutes(),
//     "s+" : this.getSeconds(),
//     "q+" : Math.floor((this.getMonth()+3)/3),
//     "S" : this.getMilliseconds()
//   };
//   if (/(y+)/.test(fmt)) {
//     fmt = fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
//   }
//   for (let k in o) {
//     if (new RegExp("("+ k +")").test(fmt)) {
//       fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
//     }
//   }
//   return fmt;
// };